<?php

namespace App\Api;

use PhalApi\Api;
use App\Domain\User\User as UserDomain;
use PhalApi\Exception\BadRequestException;
use PhpOffice\PhpWord\TemplateProcessor;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

use Qiniu\Auth;


/**
 * fibrpro报价单
 *   @desc 报价单
 */
class Fibrpro extends Api
{

    public function getRules()
    {
        return array(
            'add' => array(),
            'update' => array(),
            'transport' => array(),
            'getInvoice' => array(),
        );
    }

    /**
     * 通过price_id 拿invoice 信息
     * @desc 添加运输单据
     * @return array data 签名url
     * @exception 400 非法请求，参数传递错误
     */
    public function getInvoice()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $json = file_get_contents('php://input');
        $obj = json_decode($json, true);

        $where = array(
            "price_id" => $obj['price_id'],
            "id < ?" => $obj['id'],
        );
        $trueWhere = array();
        foreach ($where as $k=>$v){
            if($v) {
                $trueWhere[$k] = $v;
            }
        }

        if( $obj['price_id'] ){
            $r = \PhalApi\DI()->notorm->fibrpro_price_transport->where($trueWhere)->order("id DESC")->fetchOne();
            if( $r ){
                $data = json_decode($r['price_json'], true);
                return array("data" => $data, "info" => $r, 'type' => 'transport');
            }

            $r = \PhalApi\DI()->notorm->fibrpro_price->where(array(
                "id" => $obj['price_id'],
            ))->fetchOne();
            if( $r ){
                $data = json_decode($r['deposit_json'], true);
                return array("data" => $data, "info" => $r, 'type' => 'price');
            }
        }
    }


    /**
     * 更新个人信息
     * @desc 更新个人信息
     * @return array data 签名url
     * @exception 400 非法请求，参数传递错误
     */
    public function update()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $json = file_get_contents('php://input');
        $obj = json_decode($json, true);
        $data = json_decode($obj['json'], true);

        $domain = new UserDomain();

        //读出id
        $id = $obj['id'];
        if ( $id ){
            //如果有id,则，属于更新模式
            $moreInfo = array(
                'roles' => $data['roles'],
                'unit' => $data['unit'],
                'fullName' => $data['fullName'],
                'nickname' => $data['nickname'],
                'sign' => $data['sign'],
                'update_time' => date("Y-m-d H:i:s",time()),
            );
            $update = $domain->updateByUsername($data['username'], $data['password'], $moreInfo);
            if( $update ){
                return '更新成功';
            }
        }

        if( $data['username'] ){
            
            $user = $domain->getUserByUsername($data['username'], 'id');
            if ($user) {
                return '账号 '.$data['username'].' 已注册';
            }

            $moreInfo = array(
                'roles' => $data['roles'],
                'unit' => $data['unit'],
                'fullName' => $data['fullName'],
                'nickname' => $data['nickname'],
                'sign' => $data['sign'],
                'update_time' => date("Y-m-d H:i:s",time()),
                'create_time' => date("Y-m-d H:i:s",time()),
            );
            $userId = $domain->register($data['username'], $data['password'], $moreInfo);
            if( $userId ){
                return '添加成功';
            }
        }

    }

    /**
     * 添加报价单
     * @desc 添加报价单
     * @return array data 签名url
     * @exception 400 非法请求，参数传递错误
     */
    public function add()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $json = file_get_contents('php://input');
        $obj = json_decode($json, true);

        $where = array(
            "id" => $obj['id'],
        );

        $trueWhere = array();
        foreach ($where as $k=>$v){
            if($v) {
                $trueWhere[$k] = $v;
            }
        }

        $obj['update_time'] = date("Y-m-d H:i:s",time());
        $obj['date'] = date("Y-m-d",time());

        if( $obj['id'] ){
            $ishave = \PhalApi\DI()->notorm->fibrpro_price->where($trueWhere)->fetchOne();
            if($ishave){

                \PhalApi\DI()->notorm->fibrpro_price->where('id',$ishave['id'])->update($obj);
                return 'ishas';
            }
        }

        $obj['date'] = date("Y-m-d",time());
        $obj['update_time'] = date("Y-m-d H:i:s",time());
        $obj['create_time'] = date("Y-m-d H:i:s",time());

        if ( $obj['name'] ){
            return \PhalApi\DI()->notorm->fibrpro_price->insert($obj);
        }

    }


    /**
     * 添加运输单据
     * @desc 添加运输单据
     * @return array data 签名url
     * @exception 400 非法请求，参数传递错误
     */
    public function transport()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $json = file_get_contents('php://input');
        $obj = json_decode($json, true);

        $where = array(
            "id" => $obj['id'],
        );

        $trueWhere = array();
        foreach ($where as $k=>$v){
            if($v) {
                $trueWhere[$k] = $v;
            }
        }

        $obj['update_time'] = date("Y-m-d H:i:s",time());
   
        if( $obj['id'] ){
            $ishave = \PhalApi\DI()->notorm->fibrpro_price_transport->where($trueWhere)->fetchOne();
            if($ishave){
                \PhalApi\DI()->notorm->fibrpro_price_transport->where('id',$ishave['id'])->update($obj);
                return 'ishas';
            }
        }

        $obj['create_time'] = date("Y-m-d H:i:s",time());
        if ( $obj['name'] ){
            return \PhalApi\DI()->notorm->fibrpro_price_transport->insert($obj);
        }

    }


}
